package com.app.mymvp.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.mymvp.R;
import com.app.mymvp.entity.SearchHistoryEntity;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;


/**
 * Created by ${Bi} on 2016/10/13 15:32.
 */

public class SearchHistoryAdapter extends BaseAdapter {


    private Context context;
    private List<SearchHistoryEntity> list;
    private Handler handler;

    public SearchHistoryAdapter(Context context, List<SearchHistoryEntity> list, Handler handler) {
        this.context = context;
        this.list = list;
        this.handler=handler;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_search_history_item, null);
            AutoUtils.auto(convertView);
            holder.tv_history = (TextView) convertView.findViewById(R.id.tv_history);
            holder.ll_delete_history = (LinearLayout) convertView.findViewById(R.id.ll_delete_history);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_history.setText(list.get(position).getHistoryName());
        holder.ll_delete_history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message message = new Message();
                message.what = 1;
                message.arg1 = position;
                handler.sendMessage(message);
            }
        });

        return convertView;
    }

    class ViewHolder {
        TextView tv_history;
        LinearLayout ll_delete_history;
    }

}
